﻿<?php

class TimerAction
{
    public function task()
    {
		$this->log('TimerAction invoked, starting sync RnB data!');
		
		set_time_limit(0); // 最大执行时间这里设置为不限时
		
		$rnbs=$this->fetch();
		if(null == $rnbs || count($rnbs) == 0) return ;
		
		$this->update($rnbs);
		
		$this->log('Sync RnB data completed!');
	}
	
	private function update($rnbs){
		$rnbm=M("rnb");
		
		$idrs=$rnbm->field('attractionid')->select();
		
		$ids=array();
		foreach ($idrs as $id){
			array_push($ids, $id['attractionid']);
		}
		
		foreach ($rnbs as $rnb){
			$this->log('On process '.$rnb['venue_name']);
			
			$rnb['thumbnail'] = str_replace('tn', 'lg', $rnb['thumbnail']);
			
			if(array_search($rnb['attractionid'], $ids)){
				$rnbm->where('attractionid='.$rnb['attractionid'])->save($rnb);
			}
			else{
				$rnbm->add($rnb);
			}
		}
	}
	
	private function fetch(){
		$url_get='https://api.spgrestaurantsandbars.com/venue?access_token=3409c85e1235609001610c2afb07486fbcbcddd1&lang_id=cn';		
		$result = file_get_contents($url_get);
		$this->log('the result from '.$url_get.' is:\r\n'.$result);
		
		$decode = json_decode($result,true);
		
		return $decode;
	}
	
	private function log($info){
		Log::write($info, LOG::INFO);
		echo $info;
	}
}
?>